package com.eduction.modules.system.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.eduction.common.dbutil.DbHelper;
import com.eduction.modules.system.entity.ExpertGroupMap;
@Repository
public class SiteInspectionExpertGroupMapRspository {

	@Autowired
	private SiteInspectionExpertGroupMapDao siteInspectionExpertGroupMapDao;
	
	@Autowired
	private DbHelper dbhelper;
	
	/**
	 * 修改组内职务
	 */
	public void updateIdentityInGroup(String groupId,String expertId,String identity) throws SQLException{
		String sql = "update t_expert_group_map set s_identity = ? where s_expert_id = ? and s_group_id = ?";
		this.dbhelper.execute(sql, identity,expertId,groupId);
	}
	
	/**
	 * 添加专家与组关系（现场考察）
	 */
	public void saveExpertGroupMap(ExpertGroupMap e) throws SQLException{
		this.siteInspectionExpertGroupMapDao.save(e);
		String sql = "update t_expert set s_strutss=1 where p_id = ?";
		this.dbhelper.execute(sql, e.getS_expert_id());
	}


	/**
	 * 添加专家与组关系（材料）
	 */
	public void saveExpertGroupMap2(ExpertGroupMap e) throws SQLException{
		this.siteInspectionExpertGroupMapDao.save(e);
		String sql = "update t_expert set s_struts=1 where p_id = ?";
		this.dbhelper.execute(sql, e.getS_expert_id());
	}
	
	/**
	 * 踢出专家组成员（现场）
	 */
	public void deleteExpertInGroup(String expertId,String groupId) throws SQLException{
		String sql = "delete from t_expert_group_map where s_expert_id = ? and s_group_id = ?";
		this.dbhelper.execute(sql, expertId,groupId);
		
		String sql1 = "update t_expert set s_strutss=0 where p_id = ?";
		this.dbhelper.execute(sql1, expertId);
	}

	/**
	 * 踢出专家组成员（材料）
	 */
	public void deleteExpertInGroup2(String expertId,String groupId) throws SQLException{
		String sql = "delete from t_expert_group_map where s_expert_id = ? and s_group_id = ?";
		this.dbhelper.execute(sql, expertId,groupId);

		String sql1 = "update t_expert set s_struts=0 where p_id = ?";
		this.dbhelper.execute(sql1, expertId);
	}
	
	/**
	 * 查询组根据专家id
	 * @throws SQLException 
	 */
	public ExpertGroupMap queryGroupByExpertId(String expertId,String type) throws SQLException{
		String sql = " select a.* from t_expert_group_map as a left join t_group as b on a.s_group_id = b.p_id where b.s_state != '3' and a.s_expert_id = ? and s_type=?";
		return dbhelper.getBean(sql, ExpertGroupMap.class, expertId,type);
	}

    public void deleteBySchoolIdAndExpertId(String schoolId, String expertId) throws SQLException{
		String sql = "delete from t_expert_group_map where s_expert_id = ? and s_group_id = ?";
		this.dbhelper.execute(sql, schoolId,expertId);
    }

    public List<ExpertGroupMap> querySchoolByGroupId(String groupId)  throws SQLException {
		String sql = "select * from t_expert_group_map where s_group_id = ? ";
		return  dbhelper.getBeanList(sql, ExpertGroupMap.class, groupId);
    }
}
